//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class TextFieldAndButton extends JPanel {

    public TextFieldAndButton() {
        jTextField = null;
        jButtonRight = null;
        val = "N/A";
        jPanel = null;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setSize(306, 27);
        add(getJPanel(), "Center");
    }

    private JTextField getJTextField() {
        if (jTextField == null) {
            jTextField = new JTextField();
            jTextField.setText(val);
            jTextField.setPreferredSize(new Dimension(200, 23));
            jTextField.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    setVal(jTextField.getText());
                }
            });
        }
        return jTextField;
    }

    private JButton getJButtonRight() {
        if (jButtonRight == null) {
            jButtonRight = new JButton();
            jButtonRight.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/loupe.gif")));
            jButtonRight.setPreferredSize(new Dimension(100, 23));
        }
        return jButtonRight;
    }

    public void setButtonLabelLeft(String label) {
        getJTextField().setText(label);
    }

    public String getButtonLabelLeft() {
        return getJTextField().getText();
    }

    public void setButtonLabelRight(String label) {
        getJButtonRight().setText(label);
    }

    public String getButtonLabelRight() {
        return getJButtonRight().getText();
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        String old = val;
        this.val = val;
        firePropertyChange("val", old, val);
        getJTextField().setText(val);
    }

    public JTextField getTextField() {
        return getJTextField();
    }

    public JButton getButtonRight() {
        return getJButtonRight();
    }

    private JPanel getJPanel() {
        if (jPanel == null) {
            jPanel = new JPanel();
            FlowLayout flowLayout8 = new FlowLayout();
            jPanel.setLayout(flowLayout8);
            flowLayout8.setHgap(2);
            flowLayout8.setVgap(2);
            jPanel.add(getJTextField(), null);
            jPanel.add(getJButtonRight(), null);
        }
        return jPanel;
    }
    private JTextField jTextField;
    private JButton jButtonRight;
    private String val;
    private JPanel jPanel;
}
